import sys
import time
from PyQt5.QtCore import pyqtSignal, QObject, QThread
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton


# 线程1
class Thread1(QThread):
    signal = pyqtSignal(str)

    def __init__(self, text):
        super().__init__()
        self.text = text
        self.running = False

    def run(self):
        self.running = True
        while self.running:
            current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
            self.signal.emit(f"{current_time} ({self.text})")
            time.sleep(1)

    def stop(self):
        self.running = False


# 线程2
class Thread2(QThread):
    signal = pyqtSignal(str)

    def __init__(self, text):
        super().__init__()
        self.text = text
        self.running = False

    def run(self):
        self.running = True
        count = 0
        while self.running:
            self.signal.emit(f"{str(count)} ({self.text})")
            count += 1
            time.sleep(1)

    def stop(self):
        self.running = False


# 线程3
class Thread3(QThread):
    signal = pyqtSignal(str)

    def __init__(self, text):
        super().__init__()
        self.text = text
        self.running = False

    def run(self):
        self.running = True
        while self.running:
            self.signal.emit(f"Thread 3 running ({self.text})")
            time.sleep(1)

    def stop(self):
        self.running = False


# GUI界面
class MainWindow(QWidget):
    def __init__(self):
        super().__init__()

        self.thread1 = None
        self.thread2 = None
        self.thread3 = None

        self.init_ui()

    def init_ui(self):
        # 输入参数的文本框
        self.text_edit = QLineEdit()

        # 线程1控制按钮
        self.thread1_start_button = QPushButton('启动线程1')
        self.thread1_stop_button = QPushButton('停止线程1')
        self.thread1_start_button.clicked.connect(self.thread1_start)
        self.thread1_stop_button.clicked.connect(self.thread1_stop)

        # 线程2控制按钮
        self.thread2_start_button = QPushButton('启动线程2')
        self.thread2_stop_button = QPushButton('停止线程2')
        self.thread2_start_button.clicked.connect(self.thread2_start)
        self.thread2_stop_button.clicked.connect(self.thread2_stop)

        # 线程3控制按钮
        self.thread3_start_button = QPushButton('启动线程3')
        self.thread3_stop_button = QPushButton('停止线程3')
        self.thread3_start_button.clicked.connect(self.thread3_start)
        self.thread3_stop_button.clicked.connect(self.thread3_stop)

        # 显示结果的标签
        self.thread1_label = QLabel('Thread 1:')
        self.thread1_display_label = QLabel('')
        self.thread2_label = QLabel('Thread 2:')
        self.thread2_display_label = QLabel('')
        self.thread3_label = QLabel('Thread 3:')
        self.thread3_display_label = QLabel('')

        layout1 = QHBoxLayout()
        layout1.addWidget(self.thread1_start_button)
        layout1.addWidget(self.thread1_stop_button)
        layout1.addWidget(self.thread1_label)
        layout1.addWidget(self.thread1_display_label)

        layout2 = QHBoxLayout()
        layout2.addWidget(self.thread2_start_button)
        layout2.addWidget(self.thread2_stop_button)
        layout2.addWidget(self.thread2_label)
        layout2.addWidget(self.thread2_display_label)

        layout3 = QHBoxLayout()
        layout3.addWidget(self.thread3_start_button)
        layout3.addWidget(self.thread3_stop_button)
        layout3.addWidget(self.thread3_label)
        layout3.addWidget(self.thread3_display_label)

        main_layout = QVBoxLayout()
        main_layout.addWidget(self.text_edit)
        main_layout.addLayout(layout1)
        main_layout.addLayout(layout2)
        main_layout.addLayout(layout3)

        self.setLayout(main_layout)
        self.show()

    # 线程1控制方法
    def thread1_start(self):
        if not self.thread1:
            self.thread1 = Thread1(self.text_edit.text())
            self.thread1.signal.connect(self.update_thread1_display)
            self.thread1.start()

    def thread1_stop(self):
        if self.thread1:
            self.thread1.signal.disconnect()
            self.thread1.stop()
            self.thread1 = None

    # 线程2控制方法
    def thread2_start(self):
        if not self.thread2:
            self.thread2 = Thread2(self.text_edit.text())
            self.thread2.signal.connect(self.update_thread2_display)
            self.thread2.start()

    def thread2_stop(self):
        if self.thread2:
            self.thread2.signal.disconnect()
            self.thread2.stop()
            self.thread2 = None

    # 线程3控制方法
    def thread3_start(self):
        if not self.thread3:
            self.thread3 = Thread3(self.text_edit.text())
            self.thread3.signal.connect(self.update_thread3_display)
            self.thread3.start()

    def thread3_stop(self):
        if self.thread3:
            self.thread3.signal.disconnect()
            self.thread3.stop()
            self.thread3 = None

    # 更新显示的槽
    def update_thread1_display(self, text):
        self.thread1_display_label.setText(text)

    def update_thread2_display(self, text):
        self.thread2_display_label.setText(text)

    def update_thread3_display(self, text):
        self.thread3_display_label.setText(text)


app = QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec())